/********************************************************************************
 * ItemsServiceDelegator.java
 *
 * Created by Ankur Jain
 * Last modified Date: Aug 26, 2008 11:13:51 AM by Nishan
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.delegator;

import static com.printeverywhere.util.AttributeConstants.CLOSING_SESSION;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.dto.Items;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.HibernateUtil;
import com.printeverywhere.util.SearchCriteria;

/**
 * The Class ItemsServiceDelegator.
 */
public class ItemsServiceDelegator {

	/** The log. */
	private static Logger log = Logger.getLogger(ItemsServiceDelegator.class);

	/**
	 * Gets the list.
	 *
	 * @return the list
	 *
	 * @throws HibernateException the hibernate exception
	 */
	@SuppressWarnings("unchecked")
	public static List<Items> getList() throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for Bank Account without search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			List<Items> list = session.createCriteria(Items.class).list();
			transaction.commit();
			return list;
		} catch (HibernateException e) {
			log.error("Got exception when getting list for Bank Account without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}

	public static Items getItemsById(int id) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for Items by Id".intern());
		}
		Session session = HibernateUtil.getSession();
		try{
			session.beginTransaction();
			Criteria criteria = session.createCriteria(Items.class);
			criteria.add(Restrictions.eq(AttributeConstants.ITEMID , id));
			List<Items> itemslist = criteria.list();
			return ((itemslist.size() > 0) ? itemslist.get(0) : new Items());
		}catch (HibernateException e) {
			log.error("Got exception when getting list for Items by Id".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Gets the list.
	 *
	 * @return the list
	 *
	 * @throws HibernateException the hibernate exception
	 */
	@SuppressWarnings("unchecked")
	public static List<Items> getItemsList(GeneralCategories categories) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for Bank Account without search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			Criteria crit= session.createCriteria(Items.class);
			//crit.add(Restrictions.or(Restrictions.eq("generalCategories", categories), Restrictions.in("generalCategories", categories.getGeneralCategorieses())));
			List<Items> list=crit.list();
			transaction.commit();
			return list;
		} catch (HibernateException e) {
			log.error("Got exception when getting list for Bank Account without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			HibernateUtil.closeSession();
		}
	}





	/**
	 * Gets the list.
	 *
	 * @param searchCriteria the search criteria
	 *
	 * @return the list
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static List<Items> getList(SearchCriteria searchCriteria)throws HibernateException {
		if(log.isInfoEnabled()){
		log.info("Getting list for Items with search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(Items.class);
		String orderBY = searchCriteria.getOrderBy();
		if (orderBY != null) {
			if (searchCriteria.isAsc()) {
				criteria.addOrder(Order.asc(orderBY));
			} else {
				criteria.addOrder(Order.desc(orderBY));
			}
		}
		Set<String> keys = searchCriteria.getAttributeNames();
		Iterator<String> it = keys.iterator();
		String key = null;
		Object value = null;
		while (it.hasNext()) {
			key = it.next();
			value = searchCriteria.getAttribute(key);
			criteria.add(Restrictions.eq(key, value));
		}
		Transaction transaction = session.beginTransaction();
		List<Items> list;
		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for Items with search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			HibernateUtil.closeSession();
		}
		return list;
	}

	public static void main(String[] args) {
		GeneralCategories categories=CategoryManagerServiceDelegator.get(49);
		System.out.println(getItemsList(categories).size());
		Items items = ItemsServiceDelegator.getItemsById(1);
		System.out.println(items.getTitle());
		//System.out.println(getList().size());
	}

}